import requests
import csv
import json

class Spider:
    def __init__(self):
        self.champion_url = 'https://yz.lol.qq.com/v1/zh_cn/champions/{}/index.json'
        self.champions = list()
        self.champions_related = list()
        self.champions_related_repeat = list()
        
    def get_champion(self):
        url = 'https://yz.lol.qq.com/v1/zh_cn/champion-browse/index.json'
        resp = requests.get(url)
        resp_data = json.loads(resp.content.decode())
        for item in resp_data['champions']:
            self.champions.append({'name': item['name'], 'slug': item['slug']})

    def get_champion_item(self):      
        for champion in self.champions:            
            champion_url = self.champion_url.format(champion['slug'])
            resp = requests.get(champion_url)
            resp_data = json.loads(resp.content.decode())
            name = champion['name']
            for related in resp_data['related-champions']:
                tmp_left = name+related['name']
                tmp_right = related['name']+name
                if tmp_left not in self.champions_related_repeat and tmp_right not in self.champions_related_repeat:
                    self.champions_related.append([name, related['name']])
                    self.champions_related_repeat.append(tmp_left)
                    self.champions_related_repeat.append(tmp_left)

    def save_item(self):
        headers = ['Source','Target']
        with open('lol_edge.csv','w',newline='') as f:
            f_csv = csv.writer(f)
            f_csv.writerow(headers)
            f_csv.writerows(self.champions_related)

    def run(self):
        self.get_champion()
        self.get_champion_item()
        self.save_item()

if __name__ == "__main__":
    spider = Spider()
    spider.run()   